/* nrf24l01_adapter.c */
#include "app_inc.h"

/*
 * Pin connections.
 *
 * SPI_TX       PA7
 * SPI_RX       PA6
 * SPI_CLK      PA5
 * SPI_CS       PA4
 * CE(output)   PE0
 * IRQ(input)   PE1
 */

#define RF_CS_PORT  GPIOA
#define RF_CS_PIN   GPIO_PIN_4

#define RF_CE_PORT  GPIOE
#define RF_CE_PIN   GPIO_PIN_0

#define RF_IRQ_PORT GPIOE
#define RF_IRQ_PIN  GPIO_PIN_1

/*
    device.config.baudrate = 1*1000*1000;
    device.config.data_width = 8;
    device.config.mode = SPI_MODE_0 | SPI_MASTER | SPI_MSB;
*/
void NRF24L01_HW_InitIO(void)
{
    GPIO_InitPara GPIO_InitStructure;
    SPI_InitPara  SPI_InitStructure;

    /* Enable Peripheral clock */
    RCC_APB2PeriphClock_Enable( RCC_APB2PERIPH_GPIOA |
                                RCC_APB2PERIPH_GPIOE |
                                RCC_APB2PERIPH_AF , ENABLE);
    RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_SPI1,ENABLE); /* Enable clock for SPI1. */
    
    /* CS pin as GPIO output. */
    GPIO_InitStructure.GPIO_Pin = RF_CS_PIN;
    GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
    GPIO_Init(RF_CS_PORT, &GPIO_InitStructure);
    GPIO_SetBits(RF_CS_PORT, RF_CS_PIN); /* CS = 1 */

    /* Configure SPI1 pins: SCK, MISO and MOSI ---------------------------------*/
    /* Confugure SCK and MOSI pins as Alternate Function Push Pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_PIN_5 | GPIO_PIN_7 ;
    GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    /* Confugure MISO pin as Input Floating  */
    GPIO_InitStructure.GPIO_Pin = GPIO_PIN_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* SPI1 configuration */
    SPI_InitStructure.SPI_TransType = SPI_TRANSTYPE_FULLDUPLEX ;
    SPI_InitStructure.SPI_Mode = SPI_MODE_MASTER;
    SPI_InitStructure.SPI_FrameFormat = SPI_FRAMEFORMAT_8BIT ;
    SPI_InitStructure.SPI_SCKPL = SPI_SCKPL_LOW;    /* POL = */
    SPI_InitStructure.SPI_SCKPH = SPI_SCKPH_1EDGE;  /* PHA =  */
    SPI_InitStructure.SPI_SWNSSEN = SPI_SWNSS_SOFT; /* Software CS. */
    SPI_InitStructure.SPI_PSC = SPI_PSC_16;
    SPI_InitStructure.SPI_FirstBit = SPI_FIRSTBIT_MSB;
    SPI_InitStructure.SPI_CRCPOL = 7;
    SPI_Init(SPI1, &SPI_InitStructure);
    SPI_Enable(SPI1, ENABLE);

    /* CE pin as output. */
    GPIO_InitStructure.GPIO_Pin = RF_CE_PIN;
    GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
    GPIO_Init(RF_CE_PORT, &GPIO_InitStructure);
    GPIO_SetBits(RF_CE_PORT, RF_CE_PIN); /* CE = 1 */

    /* IRQ pin as input. */
    GPIO_InitStructure.GPIO_Pin = RF_IRQ_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN_FLOATING;
    GPIO_Init(RF_IRQ_PORT, &GPIO_InitStructure);
}

uint8_t NRF24L01_HW_SpiSwapData(uint8_t txData, bool enKeepCs)
{
    uint8_t tmp8;

    /* CS = 0. */
    GPIO_ResetBits(RF_CS_PORT, RF_CS_PIN); /* CS = 0. */

    while (SPI_I2S_GetBitState(SPI1, SPI_FLAG_TBE) == RESET);
    SPI_I2S_SendData(SPI1, txData);
    while (SPI_I2S_GetBitState(SPI1, SPI_FLAG_RBNE) == RESET);
    tmp8 = SPI_I2S_ReceiveData( SPI1 );
    
    if (!enKeepCs)
    {
        GPIO_SetBits(RF_CS_PORT, RF_CS_PIN); /* CS = 1. */
    }
    return tmp8;
}

void NRF24L01_HW_GpioSetCEPin(bool enLogicOne)
{
    if (enLogicOne)
    {
        GPIO_SetBits(RF_CE_PORT, RF_CE_PIN); /* CE = 1. */
    }
    else
    {
        GPIO_ResetBits(RF_CE_PORT, RF_CE_PIN); /* CE = 0. */
    }
}

bool NRF24L01_HW_GpioGetIrqPin(void)
{
    return (SET == GPIO_ReadInputBit(RF_IRQ_PORT, RF_IRQ_PIN));
}
/* EOF. */

